<?php
include(BASE_URL.'includes/admin/editors/fckeditor/fckeditor.php');
	$list = $theme->buildNavArray();
	$secondary_list = $theme->arraySecondaryNavigation();
for($col = -20; $col <= 20; $col++) {
	$weight .= "\t<option value='$col'";
	if($col == $rec['orderingid']) {
		$weight .= " selected='selected'";
	}
	$weight .= ">$col</option>\n";
}

if($_POST['save']) {
	foreach($_POST as $key => $value) {
		$_POST[$key] = mysql_real_escape_string($value);
	}
	$error = "";
	if($_POST['page-title'] == "") {
		$error[] = "Don't leave the page title blank!";
	}
	if($_POST['menu-title'] == "") {
		$error[] = "Don't leave the menu title blank!";
	}
	$time = time();
	if($error == "") {
		$parent = explode('-', $_POST['parent-item']);
		$parent_cat = $parent[0];
		$parent_id = $parent[1];
		if($parent_cat == "p") {
			$sql = "INSERT INTO ".DBTABLEPREFIX."content_pages (title, content, orderingid, menutitle, parent_id, time, secondary) VALUES ('".$_POST['page-title']."', '".$_POST['content']."', '".$_POST['weight']."', '".$_POST['menu-title']."', '".$parent_id."', '$time', '')";
		}
		elseif ($parent_cat == "s") {
			$sql = "INSERT INTO ".DBTABLEPREFIX."content_pages (title, content, orderingid, menutitle, parent_id, time, secondary) VALUES ('".$_POST['page-title']."', '".$_POST['content']."', '".$_POST['weight']."', '".$_POST['menu-title']."', '".$parent_id."', '$time', 'yes')";
		}
		$content = $admin->db_query($sql) or die(mysql_error());
		if($content) {
			$output .= $admin->message("success","Woohoo! Your Page \"".stripslashes(stripslashes(stripslashes($_POST['page-title'])))."\" was added!", "Click 'View Site' in upper right hand corner to see your page");
		}
	}
	if($error !== "") {
		$list_errors = "<ul class='error-list'>";
		foreach($error as $key => $value) {
			$list_errors .= "<li>".$value."</li>";
		}
		$list_errors .= "</ul></div>";
		$output .= $admin->message("error", "Oops! We encountered ".count($error)." error(s) when trying to add the page.", $list_errors);
	}
}
else {
		$FCKeditor_content = new FCKeditor('content') ;
		$FCKeditor_content->BasePath = THEME_URL.'includes/admin/editors/fckeditor/';
		$FCKeditor_content->Value ='';
		$FCKeditor_content->Width = '100%' ;
		$FCKeditor_content->Height = '350' ;
		$FCKeditor_content_html = $FCKeditor_content->CreateHtml();
	$form = '
						<form action="" method="post">
							<fieldset>
								<h2>Add New Page</h2>
								<div class="form-row">
									<label for="page-title" class="page-title">Page Title:</label><span><input type="text" class="page-title" name="page-title" /></span>
								</div>
								<div class="form-row">
									<label for="menu-title">Menu Title:<span class="small gray">This is what will show up in the menu.</span></label><span><input type="text" name="menu-title" /></span>
								</div>
								<div class="form-row">
									<label for="parent-item">Parent Item</label>
									<span>
										<select name="parent-item">
											<optgroup label="Main Menu">
												<option value="p-0">None (Root Item)</option>
												'.$theme->optionListFromArray($list, "p-").'
											</optgroup>
											<optgroup label="Secondary Optional Menu">
												<option value="s-0">None (Root Item)</option>
												'.$theme->optionListFromArray($secondary_list, "s-").'
											</optgroup>
										</select>
									</select>
									</span>
								</div>
								<div class="form-row">
									<label for="weight">Weight <a href="javascript:;" class="helplink" title="<strong style=\'font-size:16px;display:block;\'>Weight</strong>Weight Allows you to manage how items are ordered.<br />For example: If you weight this page 12 and another page 11, in the menu the other page would show before this one.">[Help]</a></label>
									<span>
										<select name="weight">
										'.$weight.'
										</select>
									</span>
								</div>
								<div class="form-row">
									<label class="content" for="content">Content</label>
									<span class="content">
										'.$FCKeditor_content_html.'
									</span>
								</div>
								<div class="form-row form-row-last">
									<label for="save">Save</label>
									<span><input type="submit" name="save" value="Add New Page" class="submit" /></span>
								</div>
							</fieldset>
						</form>
	';
	$output .= $form;
}
$admin->page_info['content']['page_title'] = "Add New Page";
$admin->page_info['content']['page_content']	= $output;
//Display page
include(BASE_URL.'includes/admin/admin.php');
?>